package scu.maqiang.cte;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

import scu.maqiang.fes.BVPType;
import scu.maqiang.fes.FES2Q41;
import scu.maqiang.fes.FES2Q42;
import scu.maqiang.fes.FES2Q4CTE;
import scu.maqiang.mesh.Mesh2Q4;
import scu.maqiang.mesh.Tecplot;
import scu.maqiang.numeric.Direct;
import scu.maqiang.numeric.MVO;
import scu.maqiang.numeric.NewIterSSolver;
import scu.maqiang.numeric.SRMatrix;

public class CylinderResultQ4A_DC1 {

	public static void main(String[] args) {
		double kxx = 1.0, density = 1.0, specificHeat = 1.0; 
	    double E = 2.0, Nu = 0.3, alpha = 0.05;
	    double beta = E / (1 - 2 * Nu)  * alpha;
	    
	    double theta = 0.5, T = 1.0e1, dt = T / 1000;

	    Mesh2Q4 mesh2D = new Mesh2Q4().square2D(24, 6);
	    mesh2D.scale(1.0, 0.25);
	    
	    FES2Q41 ffs = new FES2Q41(mesh2D);
	    FES2Q42 fsV = new FES2Q42(mesh2D);
	    
	    FES2Q4CTE fs = new FES2Q4CTE(mesh2D, ffs, fsV);
	    
	    SRMatrix L = new SRMatrix(fs.GetNdof());
	    SRMatrix R = new SRMatrix(fs.GetNdof());
	    
	    double[] x = new double[fs.GetNdof()];
	    double[] RHS = x.clone();
	    
	    double[] temp = new double[fs.GetNdof() / 3];
	    double[][] dispUV = new double[2][ffs.GetNdof()];
	    
	    double[] ex = temp.clone();
	    double[] ey = temp.clone();
	    double[] exy = temp.clone();
	    double[] ez = temp.clone();
	    double[] tx = temp.clone();
	    double[] ty = temp.clone();
	    double[] txy = temp.clone();
	    
	    double MM = E * (1 - Nu) / ((1 + Nu) * (1 - 2 * Nu));
	    double TRef = 8.0 * 35.0 / 13.0;
	    double AA = MM + TRef * beta * beta / (density * specificHeat);
	    
	    double[] coef = {density, specificHeat, kxx, TRef, theta, E, Nu, beta, dt};
	    fs.assembleDierctCouplingMatrix_Type1(coef, BVPType.AXISYMMETRIC, BVPType.AXISYMMETRIC,
	    		                              BVPType.AXIS_CONSIST_MASS, BVPType.AXISYMMETRIC, L, R);
	    //L.sort();
	    //System.out.println(L);

	    fs.applyBCHeat_MBN(L, 2);
	    fs.applyBCDisp_MBN(L, Direct.Y, 1, 3);
	    fs.applyBCDisp_MBN(L, Direct.X, 4);
	    //System.out.println(L);
	    NewIterSSolver solver = new NewIterSSolver(L);
	    
	    double time = 0.0;
	    
	    int lineNum = 100;
	    double dis = 1.0 / lineNum;
	    double[] lineX = new double[lineNum + 1];
	    double[] lineY = lineX.clone();
	    
	    Arrays.setAll(lineX, i -> i * dis);
	    String dir = "CylinderResultQ4A\\";
	    String XYLineFile = dir + "Line150.dat";
	    Tecplot.lineXYTime(XYLineFile, lineX, lineY, 0.0);
	    
	    String heatFile = dir + "Heat150.dat";
	    mesh2D.toTecplot(heatFile, 0.0, x);
	    
	    String deformFile =  dir + "Deform150.dat";
	    mesh2D.toTecplotDeform(deformFile, 0.0, dispUV, 0.0);
	    
	    String expansionFile = dir + "expansion150.dat";
	    String temp00File = dir + "temp00_150.dat";
	    FileWriter fw1 = null;
	    FileWriter fw2 = null;
	    try {
			fw1 = new FileWriter(expansionFile);
			fw2 = new FileWriter(temp00File);
			fw1.write("0.0\t0.0\n");
			fw2.write("0.0\t0.0\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    while(time < T) {
	    	time += dt;
	    	System.out.println("time: " + time);
	    	fs.applyBCHeat_MBN(RHS, 1.0, 2);
	    	fs.applyBCDisp_MBN(RHS, Direct.Y, 0.0, 1, 3);
		    fs.applyBCDisp_MBN(RHS, Direct.X, 0.0, 4);
		    solver.PCGSSSOR(RHS, x, 1.5, 1);
		    fs.extractUVH(x, temp, dispUV);
		    System.out.println("T min: " + MVO.min(temp)  + "\t max: " + MVO.max(temp));
		    System.out.println("U min: " + MVO.min(dispUV[0]) + "\t max: " + MVO.max(dispUV[0]));
		    System.out.println("V min: " + MVO.min(dispUV[1]) + "\t max: " + MVO.max(dispUV[1]));
		    R.mul(x, RHS);
		    
		    mesh2D.toTecplot(heatFile, time, temp);
		    mesh2D.toTecplotDeform(deformFile, time, dispUV, (1-Nu)/(alpha * (1 + Nu)));
		    Arrays.setAll(lineY, i -> ffs.getValue(temp, new double[] {lineX[i], 0.0}));
	        Tecplot.lineXYTime(XYLineFile, lineX, lineY, time);
	        try {
	        	fw1.write(time + "\t " + ffs.getValue(dispUV[0], new double[] {1.0, 0.0}) * 1.0 /(alpha * (1 + Nu))  + "\n");
				fw2.write(time + "\t " + ffs.getValue(temp, new double[] {0.0, 0.0}) + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
	    }
	    try {
	    	fw1.flush();
			fw1.close();
			fw2.flush();
			fw2.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
